package com.yimicloud.common.lang;

import java.util.List;

/**
 * Created by Justin on 2015/9/7.
 */
public class StringUtil {
    public StringUtil() {
    }

    public static boolean listIsNotEmpty(List<?> list) {
        return list != null && list.size() > 0;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() <= 0 || str.trim().length() <= 0;
    }

    public static boolean isNotEmpty(String str) {
        return str != null && str.length() > 0;
    }

    public static String strConvertUnicode(String str) {
        str = str == null?"":str;
        StringBuffer sb = new StringBuffer(str.length());
        sb.setLength(0);

        for(int i = 0; i < str.length(); ++i) {
            char c = str.charAt(i);
            sb.append("\\u");
            int j = c >>> 8;
            String tmp = Integer.toHexString(j);
            if(tmp.length() == 1) {
                sb.append("0");
            }

            sb.append(tmp);
            j = c & 255;
            tmp = Integer.toHexString(j);
            if(tmp.length() == 1) {
                sb.append("0");
            }

            sb.append(tmp);
        }

        return new String(sb);
    }

    public static String unicodeRevertStr(String str) {
        str = str == null?"":str;
        if(str.indexOf("\\u") == -1) {
            return str;
        } else {
            StringBuffer sb = new StringBuffer(str.length());

            for(int i = 0; i < str.length() - 5; i += 6) {
                String strTemp = str.substring(i, i + 6);
                String value = strTemp.substring(2);
                int c = 0;

                for(int j = 0; j < value.length(); ++j) {
                    char tempChar = value.charAt(j);
                    boolean t = false;
                    int var9;
                    switch(tempChar) {
                        case 'a':
                            var9 = 10;
                            break;
                        case 'b':
                            var9 = 11;
                            break;
                        case 'c':
                            var9 = 12;
                            break;
                        case 'd':
                            var9 = 13;
                            break;
                        case 'e':
                            var9 = 14;
                            break;
                        case 'f':
                            var9 = 15;
                            break;
                        default:
                            var9 = tempChar - 48;
                    }

                    c += var9 * (int)Math.pow(16.0D, (double)(value.length() - j - 1));
                }

                sb.append((char)c);
            }

            return sb.toString();
        }
    }
}
